<template>
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false"
             v-dialogDrag>

    <div style="margin-left: 10px;">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addGongYi">选择工艺1</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
        <el-table :data="columns" ref="dragTable" row-key="id" v-loading="loading">
          <el-table-column width="55">
            <template slot-scope="scope">
              <el-radio-group v-model="currentRow" @change="showGongxuOneList(scope.row)">
                <el-radio :label="scope.row.id">{{""}}</el-radio>
              </el-radio-group>
            </template>
          </el-table-column>
          <el-table-column label="工艺编号" align="center" prop="code"/>
          <el-table-column label="工艺名称" align="center" prop="name"/>
          <el-table-column label="工艺路线" align="center" prop="photo"/>
          <el-table-column label="工艺说明" align="center" prop="shuoming" show-overflow-tooltip/>
          <el-table-column align="center" label="工艺文件" prop="file" width="200">
            <template slot-scope="scope">
              <image-preview :src="scope.row.file || ''" height="60px" width="40px"></image-preview>
            </template>
          </el-table-column>
          <addGongyi-list ref="addGongYi" @ok="getList"></addGongyi-list>
        </el-table>

        <el-form ref="form" :model="form"  :rules="rules" v-show="showGx" :visible.sync="openGongxu" label-width="120px">
          <!--<el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addGx">添加工序</el-button>
            </el-col>
          </el-row>-->
          <el-table v-loading="loadingGongxu" :data="columnsGongxu" @selection-change="handleSelectionChange" ref="table" :max-height="tableMaxHeight" @sort-change="handleSortChange">
            <el-table-column sortable label="工序排序" align="center" prop="idx"/>
            <el-table-column sortable label="工序编码" align="center" prop="code"/>
            <el-table-column sortable label="工序名称" align="center" prop="name"/>
            <el-table-column sortable label="产品名称" align="center" prop="goodsName"/>
            <el-table-column sortable label="产品型号" align="center" prop="goodsModel"/>
            <el-table-column sortable align="center" label="是否最后一道工序" prop="islast" >
              <template slot-scope="scope" >
                <dict-tag :options="dict.type.yes_no" :value="scope.row.islast"/>
              </template>
            </el-table-column>
            <el-table-column label="图纸号" align="center" prop="shuoming"/>
            <el-table-column label="关联设备" align="center" prop="photo"/>
            <el-table-column label="使用车间id" align="center" prop="deptId"/>
            <el-table-column label="使用车间" align="center" prop="deptName"/>
            <el-table-column label="标准工时" align="center" prop="workHour"/>
            <el-table-column align="center" label="工序文件" prop="file" width="200">
              <template slot-scope="scope">
                <image-preview :src="scope.row.file || ''" height="60px" width="40px"></image-preview>
              </template>
            </el-table-column>
            <!--<el-table-column align="center" label="工序图纸" prop="photo" width="200">
              <template slot-scope="scope">
                <image-preview :src="scope.row.photo" height="60px" width="40px"></image-preview>
              </template>
            </el-table-column>-->
            <el-table-column label="备注" align="center" prop="remark"/>
            <!--<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)"
                               style="margin-left: 10px;">
                  <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                             v-hasPermi="['develop:LnGongxu:remove']">删除</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>-->
          </el-table>
          <addGxGyGoods-list ref="addGongxuList" @ok="relaxGongxuList"></addGxGyGoods-list>
          <lnGongxu-form ref="formGx" @ok="getList"></lnGongxu-form>
        </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary"  @click="submitGoodsGyGx">设 定 工 序</el-button>
      <el-button @click="cancel">取 消 设 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { getAction,addAction,deleteAction} from '@/api/manage'
  import AddGongyiList from '@/views/develop/lnGongyi/module/AddGongyiList'
  import AddGxGyGoodsList from '@/views/develop/lnGongyi/module/AddGxGyGoodsList'
  import LnGongxuForm from '@/views/develop/lnGongxu/module/LnGongxuForm'

  export default {
    name: "ChoseGxForm",
    dicts: ['yes_no'],
    components: {
      AddGongyiList,
      AddGxGyGoodsList,
      LnGongxuForm,
    },
    data() {
      return {
        title: "工序列表",
        openGongxu: false,
        open: false,
        showGx: true,
        // 遮罩层
        loading: true,
        loadingGongxu: true,
        // 选中数组
        ids: [],
        rows: [],
        columnsGongxu: [],
        rules: {
        },
        form: {},
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        currentRow: null,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 销售信息管理表格数据
        dataList: [],
        columns: [],
        saleId: null,
        goodsId: null,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20000,
          goodsId: null,
          gongyiId: null,
          code: null,
          name: null,
          /*orderByColumn: "code",
          isAsc: "asc",*/
          params: {},
        },
        queryParams2: {
          pageNum: 1,
          pageSize: 20000,
          goodsId: null,
          gongyiId: null,
          code: null,
          name: null,
          orderByColumn: "idx",
          isAsc: "ascending",
          params: {},
        },
        url: {
          list:'/lnGongyigoods/lnGongyigoods/list',
          addGy:'/lnGongyigoods/lnGongyigoods/addGy',
          listByGongyiId:'/lnGongyigongxu/lnGongyigongxu/list',
          listBySaleIdId:'/goodsSaledoGongxu/goodsSaledoGongxu/list',
          remove:'/lnGongyigongxu/lnGongyigongxu/',
          insertForPrjGygx:'/goodsSale/goodsSale/insertForPrjGygx',
        },
        tableMaxHeight: window.innerHeight - 320,
      };
    },
    created() {
      // this.intDictsForPrj();
      window.addEventListener('resize', this.getTableHeight);
    },
    destroyed() {
      window.removeEventListener('resize', this.getTableHeight);
    },
    methods: {
        /** 排序触发事件 */
     handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
      submitGoodsGyGx(){
        let that = this;
        that.$refs["form"].validate(valid => {
          if (valid) {
            const goodsOrderIn = Object.assign({});
            goodsOrderIn.saleId = that.saleId;
            goodsOrderIn.goodsId = that.queryParams.goodsId;
            goodsOrderIn.gongyiId = that.queryParams2.gongyiId;
            goodsOrderIn.columnsGongxu = that.columnsGongxu;
            addAction(that.url.insertForPrjGygx, goodsOrderIn).then(response => {
              that.open = false;
              that.$modal.msgSuccess("选择成功！");
              that.$emit('ok');
              //that.getList2();
              that.handleQuery();
            });
          }
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.$refs.formGx.handleUpdate(row);
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        deleteAction(this.url.remove, ids).then(response => {
          this.relaxGongxuList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      addGx(){
        this.$refs.addGongxuList.showGxGyGoods(this.queryParams2.gongyiId,this.saleId);
      },
      addGongYi(){
        this.$refs.addGongYi.showGy(this.queryParams.goodsId,this.saleId);
      },
      showGongxuOneList(row) {
        this.showGx=true;
        this.queryParams2.gongyiId=row.gongyiId;
        this.loadingGongxu = true;
        getAction(this.url.listByGongyiId, this.queryParams2).then(response => {
          this.columnsGongxu = response.rows;
          this.total = response.total;
          //this.openGongxu = false;
          this.loadingGongxu = false;
        });
      },
      relaxGongxuList() {
        this.showGx=true;
        this.loadingGongxu = true;
        getAction(this.url.listByGongyiId, this.queryParams2).then(response => {
          this.columnsGongxu = response.rows;
          this.total = response.total;
          //this.openGongxu = false;
          this.loadingGongxu = false;

        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          daohuoId: null,
          applyId: null,
          remark: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      getTableHeight() {
        this.tableHeight = window.innerHeight - 320
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      choseGy(row){
        this.saleId=row.id;
        this.queryParams.goodsId=row.goodsId;
        /*this.queryParams2.goodsId=row.goodsId;
        this.queryParams2.gongyiId=row.gongyiId;
        this.queryParams2.saleId=row.id;;*/
        this.open=true;
        this.getList();
        /*getAction(this.url.listBySaleIdId, this.queryParams2).then(response => {
          this.columnsGongxu = response.rows;
          this.total = response.total;
          this.loading = false;
          //this.openGongxu = false;
          this.loadingGongxu = false;
        });*/
      },
      /** 查询销售信息管理列表 */
      getList() {
        this.loading = true;
        getAction(this.url.list, this.queryParams).then(response => {
          this.columns = response.rows;
          this.total = response.total;
          if(response.rows.length > 0) {
            if (response.rows[0].gongyiId != null && response.rows[0].gongyiId != '') {
              this.queryParams2.gongyiId = response.rows[0].gongyiId;
              this.loading = false;
              this.loadingGongxu = true;
              getAction(this.url.listByGongyiId, this.queryParams2).then(responsse => {
                this.columnsGongxu = responsse.rows;
                this.total = responsse.total;
                this.loadingGongxu = false;
              });
            } else {
              this.columnsGongxu = []
              this.loading = false;
              this.loadingGongxu = false;
            }
          }else{
            this.columnsGongxu = []
            this.loading = false;
            this.loadingGongxu = false;
          }
          });
      },
      submitGy(){
        if (this.currentRow == null || this.currentRow == '' || this.currentRow == 'undefined') {
          this.$modal.msgError("请选择一条记录");
        } else {
          let params ={"goodsId":this.goodsId,"gongyiId":this.currentRow}
          getAction(this.url.addGy, params ).then(response => {
            this.$modal.msgSuccess("添加完成");
            this.open = false;
            this.$emit('ok');
          });
        }
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
    }
  };
</script>
